/*
 * 自定义角色装饰器
 */

import { applyDecorators, SetMetadata, UseGuards } from '@nestjs/common'
import { ApiBearerAuth, ApiUnauthorizedResponse } from '@nestjs/swagger'
import { JwtGuard, RoleGuard } from '@/common/guards'

export function Auth(...roles: string[]) {
  return applyDecorators(
    // 设置元数据，后续从reflector获取
    SetMetadata('roles', roles),
    // 应用 2 个守卫
    UseGuards(JwtGuard, RoleGuard),
    // 声明这个接口需要 token，文档会出现🔒图标，和需要带 token 才能测试请求
    ApiBearerAuth(),
    ApiUnauthorizedResponse({ description: 'Unauthorized' }),
  )
}

export function JwtPass() {
  return applyDecorators(SetMetadata('jwtPass', true), UseGuards(JwtGuard))
}

export function JwtNeed() {
  return applyDecorators(SetMetadata('JwtNeed', true), UseGuards(JwtGuard))
}
